#!/user/bin/env python
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @Time : 2021/05/11 18:59
# @Author : ziqi.zhang
# @Version : 1.0.0
# @Description : 读取ini文件类

import configparser


class ReadConfig:
    """配置文件读取"""

    def __init__(self):
        self.config = None

    def load_config(self):
        conf = configparser.ConfigParser()  # 读取配置文件的实例
        conf.read('../Config/env.ini', encoding='utf8')  # 获取配置文件
        s = conf.get('Switch', 'switch')
        if s == "off":
            self.config = '../Config/loc.ini'
            return self.config
        else:
            self.config = '../Config/test.ini'
            return self.config


class ReadElement:
    """读取页面元素文件"""

    def __init__(self, file_path=None):
        if file_path is None:
            self.file_path = "../Config/element.ini"
        else:
            self.file_path = file_path
        self.data = self.read_ini()

    # 获取ini文件
    def read_ini(self):
        read_ini = configparser.ConfigParser()
        read_ini.read(self.file_path)
        return read_ini

    # 通过key获取对应的value
    def get_value(self, key, section=None):
        if section is None:
            section = 'SearchElement'
        else:
            section = section
        try:
            value = self.data.get(section, key)
        except:
            value = None
        return value
